/**
 * Copyright &copy; com.smooth.common
 */
package com.smooth.site.sx.mag.biz.pic.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.smooth.common.site.common.config.Global;
import com.smooth.common.site.common.excep.SiteException;
import com.smooth.common.site.common.persistence.Page;
import com.smooth.common.site.common.web.BaseController;
import com.smooth.common.site.common.utils.StringUtils;
import com.smooth.dao.gen.bean.BizPic;
import com.smooth.dao.gen.BizPicMapper;

import com.smooth.site.sx.mag.biz.pic.service.BizPicService;

/**
 * 图片信息Controller
 * @author bm
 * @version 2019-02-26
 */
@Controller
@RequestMapping(value = "${adminPath}/pic/bizPic")
public class BizPicController extends BaseController {
	Logger log = LoggerFactory.getLogger(BizPicController.class);
	@Autowired
	private BizPicService bizPicService;
	
	@ModelAttribute
	public BizPic get(@RequestParam(required=false) String id,@RequestParam(required=false) String opt,HttpServletRequest request) {
		BizPic entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bizPicService.get(id);
		}
		if (StringUtils.isNotBlank(opt)){
			request.setAttribute("opt", opt);
		}
		if (entity == null){
			entity = new BizPic();
		}
		return entity;
	}
	
	@RequiresPermissions("pic:bizPic:view")
	@RequestMapping(value = {"list", ""})
	public String list(BizPic bizPic, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			Page<BizPic> page = bizPicService.findPage(new Page<BizPic>(request, response), bizPic); 
			model.addAttribute("page", page);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "biz/pic/bizPicList";
		}
		return "biz/pic/bizPicList";
	}
	
	@RequiresPermissions("pic:bizPic:view")
	@RequestMapping(value = "form")
	public String form(BizPic bizPic, Model model) {
		model.addAttribute("bizPic", bizPic);
		return "biz/pic/bizPicForm";
	}

	@RequiresPermissions("pic:bizPic:edit")
	@RequestMapping(value = "save")
	public String save(BizPic bizPic, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bizPic)){
			return form(bizPic, model);
		}
		try{
			bizPicService.save(bizPic);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/pic/bizPic?repage";
		}
		addMessage(redirectAttributes, "保存图片信息成功");
		return "redirect:"+Global.getAdminPath()+"/pic/bizPic?repage";
	}
	
	@RequiresPermissions("pic:bizPic:edit")
	@RequestMapping(value = "delete")
	public String delete(BizPic bizPic, RedirectAttributes redirectAttributes) {
		try{
			bizPicService.delete(bizPic);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/pic/bizPic?repage";
		}
		addMessage(redirectAttributes, "删除图片信息成功");
		return "redirect:"+Global.getAdminPath()+"/pic/bizPic?repage";
	}

}